package xhwl.insurance.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import xhwl.insurance.R;
import xhwl.insurance.http.bean.resp.CardDetailResq;
import xhwl.insurance.ui.CardDetailsActivity;
import xhwl.insurance.util.ImgTools;

/**
 * Created by Administrator on 2017/11/24.
 * HMO售卡统计中的售卡详情页
 */

public class CardNewDetailsAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<CardDetailResq> mlistVo;

    public CardNewDetailsAdapter(Context mContext, ArrayList<CardDetailResq> mlistVo) {
        this.mContext=mContext;
        this.mlistVo=mlistVo;
    }

    @Override
    public int getCount() {
        return mlistVo.size();
    }

    @Override
    public Object getItem(int position) {
        return mlistVo.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
       ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.card_detail_new_item, null);
            holder = new ViewHolder();
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);
            holder.card_title = (TextView) convertView.findViewById(R.id.card_title);
            holder.card_num = (TextView) convertView.findViewById(R.id.card_num);
            holder.join_time = (TextView) convertView.findViewById(R.id.join_time);
            holder.yuanzhu_time = (TextView) convertView.findViewById(R.id.yuanzhu_time);
            holder.card_image = (ImageView) convertView.findViewById(R.id.card_image);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tv_name.setText(mlistVo.get(position).getPERSON_NAME());
        holder.tv_phone.setText(getIphone(mlistVo.get(position).getPERSON_MOBILE().toString().trim()));

        holder.card_title.setText(mlistVo.get(position).getCARD_NAME());
        holder.card_num.setText(mlistVo.get(position).getCARD_COUNT()+"张");

        String jtime[] = mlistVo.get(position).getBUY_DATE().split(" ");//购买日期
        holder.join_time.setText("加入时间:"+jtime[0]);
        String sTime[] = mlistVo.get(position).getSTART_DATE().split(" ");//生效期
        String eTime[] = mlistVo.get(position).getEND_DATE().split(" ");//结束日期
        holder.yuanzhu_time.setText("援助有限期:"+sTime[0]+"至"+eTime[0]);

        ImgTools.getInstance().getImgFromNetByUrl(mlistVo.get(position).getCARD_PIC(),holder.card_image , R.drawable.icon_fail);
        return convertView;
    }

    class ViewHolder {
        TextView tv_name;//姓名
        TextView tv_phone;//手机号
        TextView card_title;//卡的类型
        TextView card_num;//售卡数量
        TextView join_time;//加入时间
        TextView yuanzhu_time;//援助有限期
        ImageView card_image;//卡图片
    }

    private String getIphone(String pNumber) {
        String phone = "";
        if (!"".equals(pNumber) && pNumber.length() > 6){
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < pNumber.length(); i++) {
                char c = pNumber.charAt(i);
                if (i >= 3 && i <= 6) {
                    sb.append('*');
                } else {
                    sb.append(c);
                }
            }
            phone = sb.toString().trim();
        }

        return phone;
    }
}
